package cn.zhangjie.pet.service.impl;

import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.service.IBaiduAiAuditService;
import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.basic.util.CodeGenerateUtils;
import cn.zhangjie.basic.util.DistanceUtils;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.basic.util.Point;
import cn.zhangjie.order.domain.OrderPetAcquisition;
import cn.zhangjie.order.mapper.OrderPetAcquisitionMapper;
import cn.zhangjie.org.domain.Employee;
import cn.zhangjie.org.domain.Shop;
import cn.zhangjie.org.mapper.EmployeeMapper;
import cn.zhangjie.org.mapper.ShopMapper;
import cn.zhangjie.pet.domain.Pet;
import cn.zhangjie.pet.domain.SearchMasterMsg;
import cn.zhangjie.pet.domain.SearchMasterMsgAuditLog;
import cn.zhangjie.pet.dto.AcceptDto;
import cn.zhangjie.pet.mapper.PetMapper;
import cn.zhangjie.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zhangjie.pet.mapper.SearchMasterMsgMapper;
import cn.zhangjie.pet.query.SearchMasterMsgQuery;
import cn.zhangjie.pet.service.ISearchMasterMsgAuditLogService;
import cn.zhangjie.pet.service.ISearchMasterMsgService;

import cn.zhangjie.user.domain.User;
import cn.zhangjie.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * (SearchMasterMsg)表服务实现类
 *
 * @author makejava
 * @since 2022-08-09 14:23:47
 */
@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class SearchMasterMsgServiceImpl extends BasicServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService  {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    /**
     * 无极限树
     *
     * @author makejava
     * @since 2022-08-06 18:34:41
     */
     @Override
    public List<SearchMasterMsg> deptTree() {
        return null;
    }

    @Override
    public void publish(SearchMasterMsg smm, Long id) {
        //先添加到数据库t_serarchmastermsg stste：0待审核
        //user_id  shop_id
        //通过logininfoid查询userid
        User user = userMapper.LoadByLogininfoId(id);
        smm.setUserId(user.getId());
        smm.setNote("待审核！");
        //shop_id先计算最近的店铺
        //获取当前的地址point
        Point point = DistanceUtils.getPoint(smm.getAddress());
        Shop nearestShop = DistanceUtils.getNearestShop(point, shopMapper.loadAll());
        smm.setShopId(nearestShop.getId());
        searchMasterMsgMapper.save(smm);

        //2审核
        Boolean text = baiduAiAuditService.textAudit(smm.getTitle() + smm.getName() + smm.getAddress() + smm.getNote());
        //Boolean img = baiduAiAuditService.textAudit(smm.getResources());   && img

        if(text){
            smm.setState(1);
            smm.setNote("待处理");
            searchMasterMsgMapper.update(smm);
            SearchMasterMsgAuditLog smml = new SearchMasterMsgAuditLog();
            smml.setState(1);
            smml.setMsgId(smm.getId());
            smml.setNote("待处理状态");
            smml.setAuditTime(new Date());
            searchMasterMsgAuditLogMapper.save(smml);
            //短信邮箱
        }else {
            smm.setState(0);
            smm.setNote("驳回");
            searchMasterMsgMapper.update(smm);
            SearchMasterMsgAuditLog smml = new SearchMasterMsgAuditLog();
            smml.setState(0);
            smml.setMsgId(smm.getId());
            smml.setNote("驳回");
            smml.setAuditTime(new Date());
            searchMasterMsgAuditLogMapper.save(smml);
            throw new BusinssException("信息异常，请重新输入！");
        }

    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Long id) {
         //查询到用户id
        User user = userMapper.LoadByLogininfoId(id);
        query.setUserId(user.getId());
        return  super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, Long id) {
         //通过logininfo查询employee
        Employee employee = employeeMapper.loadByLogininId(id);
        query.setState(1);
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }

    @Override
    public void accept(AcceptDto dto, Long id) {
        String note = dto.getNote();
        Long msgId = dto.getMsgId();
        Long handler = dto.getHandler();
        // 根据smmid查询订单，如果查的到，该订单正在处理中，
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadBySmmId(msgId);
        if(order!=null){ // 之前接单了
            throw  new BusinssException("订单正在处理中。。。！");
        }

        //  1. 生成一个pet : 根据searchMasterMsg生成。
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(msgId);
        System.out.println(searchMasterMsg);

        Pet pet = searchMasterMsg2Pet(searchMasterMsg);
        // 保存,保存id
        petMapper.save(pet);

        // 2. 生成一个收购订单：
        OrderPetAcquisition opa = ininOrderPetAcquisition(dto,pet,searchMasterMsg);
        // 3. 保存订单
        orderPetAcquisitionMapper.save(opa);

    }
    /**
     * 生成一个订单对象
     * @param dto
     * @param pet
     * @return
     */
    private OrderPetAcquisition ininOrderPetAcquisition(AcceptDto dto, Pet pet,SearchMasterMsg smm) {

        // 创建一个订单对象，赋值
        OrderPetAcquisition order = new OrderPetAcquisition();

        // 随机生成一个订单编号
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getId()));
        // 摘要
        order.setDigest("【"+smm.getTitle()+"】");
        // 最后确认时间 定时任务
        order.setLastcomfirmtime(new Date(System.currentTimeMillis()+2*24*60*60*1000));

        order.setState(0);// 待处理
        order.setPrice(smm.getPrice());
        order.setPet_id(pet.getId());
        order.setUser_id(smm.getUserId());
        System.out.println(smm.getUserId());
        order.setShop_id(smm.getShopId());
        System.out.println(smm.getShopId());
        order.setAddress(smm.getAddress());
        order.setEmp_id(dto.getHandler());
        order.setSearch_master_msg_id(dto.getMsgId());
        return order;
    }


    //通过searchMasterMsg生成一个pet
    private Pet searchMasterMsg2Pet(SearchMasterMsg smm) {
        Pet pet = new Pet();
        pet.setName(smm.getName());
        pet.setCostprice(smm.getPrice());
        pet.setType_id(smm.getPetType());
        pet.setResources(smm.getResources());
        pet.setShop_id(smm.getShopId());
        pet.setSearch_master_msg_id(smm.getId());
        return pet;
    }


}
